package com.lbn.monitor.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.free.framework.lib.utils.Dp2Px;
import com.lbn.monitor.R;

import indi.free.widgets.custom_recycler_view.BaseRecyclerAdapter;


/**
 * Created by Administrator on 2016/11/2.
 */

public class ListPopupWindow {

    private PopupWindow mPopupWindow;

    private RecyclerView recyclerView;

    private int rootViewWidth,rootViewHeight;

    public void onCreatePopup(Context context) {

        View popupView = LayoutInflater.from(context).inflate(R.layout.list_popup_window, null);

        mPopupWindow = new PopupWindow(popupView, Dp2Px.dip2px(context, 120), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null));

        rootViewWidth = popupView.getMeasuredWidth();
        rootViewHeight = popupView.getMeasuredHeight();

        recyclerView = (RecyclerView) popupView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        recyclerView.setAdapter(adapter);
    }

    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        recyclerView.setLayoutManager(layoutManager);
    }


    public void show(View v) {
        int[] location = new int[2];
        v.getLocationOnScreen(location);
//        mPopupWindow.showAsDropDown(v);
        mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - rootViewWidth / 2, location[1] - rootViewHeight);
//        mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    }


    public void dismiss() {
        mPopupWindow.dismiss();
    }


}
